package cn.talkimages.framework.utils.page;

public class PageUtil {

	/**
	 * get the page start offset. begin with 'zero'.
	 * 
	 * @param pageNo
	 * @param pageSize
	 * @return Integer
	 */
	public static Integer getOffset(Integer pageNo, Integer pageSize) {

		// can not previous first page
		if (pageNo < 1)
			pageNo = 1;

		return (pageNo - 1) * pageSize;
	}

	public static Integer getOffsetLimited(Integer pageNo, Integer pageSize,
			Long total) {

		// can not previous first page
		if (pageNo < 0)
			pageNo = 0;

		Integer pageCount = getPageCount(pageSize, total);

		// over the last page
		if (pageNo > pageCount) {
			pageNo = pageCount;
		}

		return getOffset(pageNo, pageSize);
	}

	/**
	 * get the amount of page
	 * 
	 * @param pageSize
	 * @param total
	 * @return Integer
	 */
	public static Integer getPageCount(Integer pageSize, Long total) {

		Integer pageCount = total.intValue() / pageSize;

		return total.intValue() % pageSize == 0 ? pageCount : pageCount + 1;
	}
}
